package com.zhang.springcloud.app1.config;

import org.springframework.cloud.openfeign.FeignFormatterRegistrar;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 参考：
 * Feign发送Get请求时，采用POJO对象传递参数的最终解决方案 Request method 'POST' not supported （附带其余好几个坑）
 * https://www.cnblogs.com/softidea/p/11088460.html
 *
 * @author zhanghaolian
 * @date 2021/6/23 1:51
 */
@Component
public class DateFormatRegister implements FeignFormatterRegistrar {

    public DateFormatRegister(){
    }

    @Override
    public void registerFormatters(FormatterRegistry registry) {
        registry.addConverter(Date.class, String.class, new Date2StringConverter());
    }

    private class Date2StringConverter implements Converter<Date,String> {

        @Override
        public String convert(Date source) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(source);
        }

    }
}